Ext.define('qjtravel.view.Home', {
    extend: 'Ext.Panel',
    xtype: 'home',
    requires: [
        'Ext.carousel.Carousel',
        'Ext.List', 'Ext.TitleBar'
    ],
    config: {
        style: 'background:#000',
        layout: 'vbox',
        items: [
            {
                xtype: 'toolbar',
                ui: 'dark',
                docked: 'top',
                title: ' 首页',
                flex: 1
            },
            {
                xtype: 'carousel',
                store: 'DistrictAdPic',
                id: 'homeCarousel',
                style: 'background:#000',
                flex: 4,
                listeners: {
                    initialize: function (carousel) {
                        var bodyW = document.body.clientWidth;
                        carousel.setHeight(bodyW / 3 * 2);
                        var adpic = Ext.getStore('HomeAdpic');
                        var dataLength = '';
                        if (adpic.find('cadid', 6) == -1) {
                            adpic.load({
                                params: {
                                    cadid: 6
                                },
                                callback: function (data) {
                                    var myAdPicItems = [];
                                    dataLength = data.length;
                                    Ext.each(data, function (adPic) {
                                        myAdPicItems.push({
                                            xtype: 'panel',
                                            items: {
                                                html: "<img width='100%' height='100%' src='" + adPic.get('picUrl') + "'/ >" + '<div class="carouselText">' + adPic.get('name') + '</div>'
                                            },
                                            listeners: {
                                                painted: function (panel) {
                                                    panel.on('tap', function () {
                                                        this.getParent().fireEvent('onTap', adPic.get('cdaid'), adPic.get('linkProduct'));
                                                        return false;
                                                    }, this);
                                                }
                                            }
                                        });
                                    });
                                    carousel.setItems(myAdPicItems);
                                    carousel.setActiveItem(0);
                                }
                            });
                        }
                        setInterval(function () { //设定2秒钟切换一次
                            carousel.next(); //切换的方法
                            if (carousel.getActiveIndex() === (dataLength - 1)) {
                                carousel.setActiveItem(0);
                            }
                        }, 3000);
                    }
                }
            },
            {
                xtype: 'dataview',
                id: 'click_category',
                store: 'Category',
                emptyText: '',
                //style:"border:1px solid #fff",
                flex: 2,
                scrollable: null,
                listeners: {
                    painted: function (dataview) {
                        var category = Ext.getStore('Category');
                        if (category.find('cadid', 6) == -1) {
                            category.load({
                                params: {
                                    cadid: 6
                                }
                            });
                        }
                    }
                }
            }
        ],
        listeners: {
            /*initilize:在控件初始化时候调用
             panited:
             */
            initialize: function () {
                this.categroyResizeFn(15);
            },
            resize: function () {
                setTimeout(function () {
                 //   console.log(document.body.clientWidth)
                }, 1000)
                var clientW = document.body.clientWidth;
                if (clientW > 901) {
                    this.categroyResizeFn(5);
                } else {
                    this.categroyResizeFn(15);
                }

            }
        }
    },
    categroyResizeFn: function (baseH) {
        var clientHeight = document.body.clientHeight;
        var c = clientHeight - 90;
        Ext.getCmp('homeCarousel').setHeight(c * 0.55);
        Ext.getCmp('click_category').setHeight(c * 0.45);
        var dataH = Ext.getCmp("click_category").getHeight();
       // console.log("baseH====" + baseH)
        var dataViewDivHeight = dataH * 0.5 - baseH;
        Ext.getCmp("click_category").setItemTpl('<div class="homeCategoryItem" style="height:' + dataViewDivHeight + 'px"><img src="{iconUrl}"/><span class="homeCategoryItemSpan">{name}</span></div>');
    }
});